package cn.xd.base.persistence.repository.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.xd.base.basic.type.GlobalAppUserId;
import cn.xd.base.domain.entity.AppUserThirdParty;
import cn.xd.base.domain.entity.GlobalAppUser;
import cn.xd.base.domain.repository.AppUserThirdPartyRepository;
import cn.xd.base.domain.repository.GlobalAppUserRepository;
import cn.xd.base.persistence.dos.AppUserThirdPartyConverter;
import cn.xd.base.persistence.dos.AppUserThirdPartyDO;
import cn.xd.base.persistence.dos.GlobalAppUserConverter;
import cn.xd.base.persistence.dos.GlobalAppUserDO;
import cn.xd.base.persistence.mps.AppUserThirdPartyMpService;
import cn.xd.base.persistence.mps.GlobalAppUserMpService;
import cn.xd.core.ddd.db.DbRepositorySupport;
import cn.xd.core.ddd.diff.EntityDiff;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class GlobalAppUserRepositoryImpl extends DbRepositorySupport<GlobalAppUser, GlobalAppUserId> implements GlobalAppUserRepository {

    @Resource
    private GlobalAppUserMpService globalAppUserMpService;

    @Resource
    private AppUserThirdPartyMpService appUserThirdPartyMpService;

    @Resource
    private AppUserThirdPartyRepository appUserThirdPartyRepository;

    @Override
    protected void onInsert(GlobalAppUser aggregate) {
        globalAppUserMpService.findByPhone(aggregate.getPhone().getValue(), null).isNotEmptyThrow("此手机号已被注册");
        globalAppUserMpService.saveResult(aggregate, GlobalAppUserConverter.INSTANCE)
                .successAndFailThrow((data) -> aggregate.setId(GlobalAppUserId.of(data.getId())));
        if (CollUtil.isNotEmpty(aggregate.getThirdPartyList())) {
            for (AppUserThirdParty appUserThirdParty : aggregate.getThirdPartyList()) {
                appUserThirdParty.setUserId(aggregate.getId());
                appUserThirdPartyRepository.save(appUserThirdParty);
            }
        }
    }

    @Override
    public GlobalAppUser getGlobalAppUserByPhone(String phone) {
        GlobalAppUserDO globalAppUserDO = globalAppUserMpService.findByPhone(phone, null).get();
        if (ObjUtil.isEmpty(globalAppUserDO)) {
            return null;
        }
        GlobalAppUser globalAppUser = GlobalAppUserConverter.INSTANCE.fromData(globalAppUserDO);
        List<AppUserThirdPartyDO> thirdPartyDOList = (List<AppUserThirdPartyDO>) appUserThirdPartyMpService.listByUserId(globalAppUserDO.getId()).get();
        globalAppUser.setThirdPartyList(AppUserThirdPartyConverter.INSTANCE.fromData(thirdPartyDOList));
        attach(globalAppUser);
        return globalAppUser;
    }

    @Override
    protected GlobalAppUser onSelect(GlobalAppUserId globalAppUserId) {
        GlobalAppUserDO globalAppUserDO = globalAppUserMpService.getById(globalAppUserId.getValue());
        GlobalAppUser globalAppUser = GlobalAppUserConverter.INSTANCE.fromData(globalAppUserDO);
        List<AppUserThirdPartyDO> thirdPartyDOList = (List<AppUserThirdPartyDO>) appUserThirdPartyMpService.listByUserId(globalAppUserDO.getId()).get();
        globalAppUser.setThirdPartyList(AppUserThirdPartyConverter.INSTANCE.fromData(thirdPartyDOList));
        return globalAppUser;
    }

    @Override
    protected void onUpdate(GlobalAppUser aggregate, EntityDiff diff) {
        if (diff.isSelfModified()) {
            GlobalAppUserDO bean = diff.getBean(GlobalAppUserDO.class);
            if (ObjUtil.isNotEmpty(bean.getPhone())) {
                globalAppUserMpService.findByPhone(aggregate.getPhone().getValue(), aggregate.getId().getValue()).isNotEmptyThrow("此手机号已被注册");
            }
            globalAppUserMpService.updateById(bean);
        }
        this.updateList(GlobalAppUser::getThirdPartyList, diff, AppUserThirdPartyConverter.INSTANCE);
    }

    @Override
    protected void onDelete(GlobalAppUser aggregate) {
        aggregate.markAsDelete();
        this.save(aggregate);
    }

}
